﻿using System;
using System.Net;
using System.IO;
using System.Text;

namespace Sausage.Common.Pinyin
{
    public class PinyinUnit
    {
        /// <summary>
        /// 从给定的二进制读取对象中加载拼音单元。
        /// </summary>
        /// <param name="binaryReader">包含拼音单元的二进制读取对象。</param>
        /// <returns>从给定的二进制读取对象中加载的拼音单元</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:46    创建
        /// </history>
        public static PinyinUnit Load(BinaryReader binaryReader)
        {
            //创建拼音单元
            PinyinUnit unit = new PinyinUnit();

            //读取拼音数据
            byte[] bytes = binaryReader.ReadBytes(7);

            //读取拼音
            unit.Pinyin = UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length);

            //用于保存需要去掉的字符
            char[] trimChars = new char[1];

            //去掉空格
            unit.Pinyin = unit.Pinyin.TrimEnd(trimChars);

            //返回拼音单元
            return unit;
        }

        /// <summary>
        /// 将拼音单元保存到给定的二进制写入器中。
        /// </summary>
        /// <param name="binaryWriter">用于写入拼音单元的二进制写入器。</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:55    创建
        /// </history>
        public void Save(BinaryWriter binaryWriter)
        {
            //用于保存字节
            byte[] bytes = new byte[7];

            //获得字符串的编码
            UTF8Encoding.UTF8.GetBytes(this.Pinyin, 0, this.Pinyin.Length, bytes, 0);

            //写入拼音信息
            binaryWriter.Write(bytes);
        }

        #region ---Property

        /// <summary>
        /// 获得或者设置拼音信息
        /// </summary>
        /// <value>拼音信息</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/05/27 15:45    创建
        /// </history>
        public string Pinyin
        {
            get;
            set;
        }

        #endregion
    }
}
